<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card class="tree-card">
        <!-- 用了一个行列布局 -->
        <tree-tool
          :tree-node="company"
          :istool="true"
          @add-depts="hSwitchAddDepts"
        />
        <hr>
        <!-- :expand-on-click-node="false" -->
        <el-tree
          :data="departs"
          :props="defaultProps"
        >
          <tree-tool
            slot-scope="{ data }"
            :tree-node="data"
            style="width:100%"
            @update-departments="hUpdateDepartments"
            @add-depts="hSwitchAddDepts"
            @edit-depts="hEditDepts"
          />
        </el-tree>
      </el-card>
      <add-depts
        ref="editDepts"
        :show-dialog.sync="isShowDialog"
        :current-node="addNode"
        @update-depts="getDepartments"
      />
    </div>
  </div>
</template>

<script>
import TreeTool from './components/treeTool'
import AddDepts from './components/addDepts'
import { getDepartments } from '@/api/departments'
import { transData } from '@/utils/index.js'
export default {
  components: {
    TreeTool,
    AddDepts
  },
  data () {
    return {
      defaultProps: {
        label: 'name'
      },
      company: {
        id: '',
        name: '',
        manager: '负责人'
      },
      departs: [],
      isShowDialog: false,
      addNode: {}
    }
  },
  created () {
    this.getDepartments()
  },
  methods: {
    async getDepartments () {
      const res = await getDepartments()
      // console.log(res)
      this.company.name = res.companyName
      this.departs = transData(res.depts, '')
      // console.log(res2)
    },
    // 点击删除触发事件，重新获取数据
    hUpdateDepartments () {
      // console.log(123)
      this.getDepartments()
    },
    // 点击切换弹层显示
    hSwitchAddDepts (node) {
      this.isShowDialog = true
      this.addNode = node
    },
    hEditDepts (node) {
      this.addNode = node
      this.$refs.editDepts.deptsList(this.addNode.id)
      // console.log(this.$refs)
      this.isShowDialog = true
    }
  }
}
</script>

<style scoped>
.tree-card {
  padding: 30px 140px;
  font-size: 14px;
}
</style>
